<?php
// 部门模块
class DeptAction extends CommonAction {
	
	//自定义单一模块无须认证的方法
	public function _noAuth()
	{
		if(in_array(ACTION_NAME,array('treeArray','getDeptNameList'))) return true;
		else return false;
	}

	public function _before_index() {
		$this->_getButton();//工具栏按钮
	}
	
	// 获取配置类型
	public function checkChild() {
		$name = $this->getActionName();
		$model = D($name);
		$pk = $model->getPk();
		$id = $_REQUEST [$pk];
		$list = $model->where("pid="+$id)->select();
		//dump($list);
		if ($list !== false) {
		     $this->error('下面还有子记录,是否也册除');
        }
		
	}
	public function getDeptNameList()
	{
		$name = $this->getActionName();
		$model = D($name);
		$rows = $model->field('name as text,id')->select();
		//$result=array('Rows'=>$rows,'Total'=>count($rows));
		echo json_encode($rows);
	}
	
	public function getGridTreeList()
	{
		$name = $this->getActionName();
		$model = D($name);
		$rows = $model->where("pid=0")->select();
		
		//$result=array('Rows'=>$rows,'Total'=>count($rows));
		echo json_encode($rows);
	}
	
	function getChildren($mode,$pid){
		$row= $mode->where("pid="+$pid)->select();
		echo $row;
	}
	
	public function treeArray() {
		$name = $this->getActionName();
		$model = D($name);
		$rows = $model->select();
		$result = array();
		$I = array();
	
		foreach($rows as $val) {
			if($val['pid'] == 0) {
				$i = count($result);
				$val['isextend']=false;
				$result[$i] = $val;
				$I[$val['id']] = & $result[$i];
			} else {
				$i = count($I[$val['pid']]['children']);
				$val['isextend']=false;
				$I[$val['pid']]['children'][$i] = $val;
				$I[$val['id']] = & $I[$val['pid']]['children'][$i];
			}
		}
		//exit(print_r($result));
		echo json_encode(array('Rows'=>$result));
	}
	
}
?>